﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Profile;
using COFE.Core.Utils;
using COFE.Core;
using COFE.Core.Validater;

namespace COFE.Data.Profile
{
    public class DBPropertyDictionary : PropertyDictionary
    {
        #region Constructor

        #endregion

        #region Methods

        public override void RegisterProperty(object p)
        {
            base.RegisterProperty(p);
            COFEDBShared.DBOperations.UpdateDatabase((context) =>
                {
                    bool propertyFound = context.Properties.Any(cp => cp.PropertyId == (int)p);
                    if (!propertyFound)
                    {
                        Type defaultType = PropertyTypeExtension.GetDefaultPropertyType(p);
                        string defaultTypeString = defaultType == null ? "Unknown" : defaultType.ToString();
                        if (defaultTypeString.ToLower().StartsWith("system.func"))
                            defaultTypeString = "Func";
                        var property = Property.CreateProperty((int)p, GetPropertyString(p),
                            MetadataExtension.IsMetadata(p), ResourceExtension.IsResource(p),
                            defaultTypeString);
                        context.Properties.AddObject(property);
                        context.SaveChanges();
                    }                    
                });

        }

        public override void RegisterProperties<T>()
        {
            base.RegisterProperties<T>();

            List<object> properties = new List<object>();
            foreach (var value in Enum.GetValues(typeof(T)))
                properties.Add(value);

            try
            {

                foreach (object p in properties)
                {
                    COFEDBShared.DBOperations.UpdateDatabase((context) =>
                        {
                            return context.Properties.Any(cp => cp.PropertyId == (int)p);
                        },
                        (context, propertyFound) =>
                        {
                            if (!propertyFound)
                            {
                                Type defaultType = PropertyTypeExtension.GetDefaultPropertyType(p);
                                string defaultTypeString = defaultType == null ? "Unknown" : defaultType.ToString();

                                return Property.CreateProperty((int)p, GetPropertyString(p),
                                    MetadataExtension.IsMetadata(p), ResourceExtension.IsResource(p),
                                    defaultTypeString);
                            }
                            return null;
                        },
                        (context, propertyFound, property) =>
                        {
                            if (!propertyFound)
                            {
                                context.Properties.AddObject(property);
                                context.SaveChanges();
                            }
                        }

                        );

                }
               
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
        }


        #endregion
    }
}
